//package com.vhall.component.plugin.common.aspect;
//
//import com.alibaba.fastjson.JSON;
//import com.vhall.component.framework.common.exception.BusinessException;
//import com.vhall.component.plugin.common.annotation.TokenAuth;
//import com.vhall.component.plugin.common.constant.RedisKey;
//import com.vhall.component.plugin.common.exception.BizErrorCode;
//import com.vhall.component.plugin.common.support.AdmToken;
//import com.vhall.component.plugin.common.support.AdmTokenThreadLocal;
//import com.vhall.component.plugin.common.support.PermissionSupport;
//import com.vhall.component.plugin.common.utils.JsonUtil;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.commons.lang3.StringUtils;
//import org.aspectj.lang.ProceedingJoinPoint;
//import org.aspectj.lang.annotation.Around;
//import org.aspectj.lang.annotation.Aspect;
//import org.aspectj.lang.annotation.Pointcut;
//import org.aspectj.lang.reflect.MethodSignature;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.data.redis.core.RedisTemplate;
//import org.springframework.stereotype.Component;
//import org.springframework.util.Assert;
//import org.springframework.web.context.request.RequestContextHolder;
//import org.springframework.web.context.request.ServletRequestAttributes;
//
//import javax.servlet.http.HttpServletRequest;
//import java.lang.reflect.Method;
//
///**
// * token切面
// *
// * @author longzhen.gong
// * @date 2021/6/21 10:41
// */
//@Slf4j
//@Aspect
//@Component
//public class AdmTokenAspect extends BaseDataAspect {
//
//    @Autowired
//    private RedisTemplate<String, String> redisTemplate;
//
//    @Autowired
//    private PermissionSupport support;
//
//    @Pointcut("@annotation(com.vhall.component.plugin.common.annotation.AdmTokenAuth) "
//            + "|| @within(com.vhall.component.plugin.common.annotation.AdmTokenAuth)")
//    public void pointCut() {
//        // Do nothing
//    }
//
//    @Around("pointCut()")
//    public Object tokenBefore(ProceedingJoinPoint joinPoint) throws Throwable {
//        //获取方法参数值数组
//        Object[] args = joinPoint.getArgs();
//        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//        Assert.notNull(servletRequestAttributes, "请求参数异常");
//        HttpServletRequest request = servletRequestAttributes.getRequest();
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        Class<?> targetClass = joinPoint.getTarget().getClass();
//        Method method = methodSignature.getMethod();
//        AdmTokenAuth tokenAuth = method.getAnnotation(AdmTokenAuth.class);
//        if (tokenAuth == null) {
//            tokenAuth = targetClass.getAnnotation(AdmTokenAuth.class);
//        }
//        String permi = tokenAuth.value();
//        String param = request.getHeader("token");
//
//        AdmToken tk;
//        String tokenText = "";
//
//        if (validMockFlag(request)) {
//            // 测试环境，且是mock数据的话
//            tokenText = "{\"adminId\":1,\"adminName\":\"admin\",\"createdAt\":\"2021-12-31T19:50:17\",\"email\":\"\",\"lastIp\":\"1.119.193.36\",\"lastTime\":\"2023-04-21T16:51:57\",\"loginNum\":1222,\"mobile\":\"\",\"nickName\":\"ADMIN\",\"password\":\"0a6ead819a1df923833662c9ebaba115d6ed04dfbc4c9844a3331b532e637683\",\"roleId\":11,\"salt\":\"k8jex1\",\"status\":1,\"token\":\"1186fb62ffc80ccf\",\"tokenExpire\":\"2023-04-24T17:04:24.156953000\",\"updatedAt\":\"2023-04-21T17:31:14\"}";
//        } else {
//            if (StringUtils.isAllEmpty(param)) {
//                throw new BusinessException(BizErrorCode.AUTH_ADM_TOKEN_INVALID);
//            }
//            if (StringUtils.isAllEmpty(param)) {
//                return joinPoint.proceed(args);
//            }
//
//            String token = redisTemplate.opsForValue().get(RedisKey.AUTH_TOKEN + param);
//            if (StringUtils.isBlank(token)) {
//                throw new BusinessException(BizErrorCode.AUTH_ADM_TOKEN_INVALID);
//            }
//            if (StringUtils.isBlank(token)) {
//                return joinPoint.proceed(args);
//            }
//            tokenText = JSON.parse(token).toString();
//        }
//
//
//        try {
//            tk = JsonUtil.objectFromJSONString(tokenText, AdmToken.class);
//            if (StringUtils.isNotBlank(permi) && support.lacksPermi(permi, tk)) {
//                throw new BusinessException(BizErrorCode.BIZ_NO_PERMISSION);
//            }
//            AdmTokenThreadLocal.setToken(tk);
//            return joinPoint.proceed(args);
//        } catch (Throwable e) {
//            log.error("", e);
//            throw e;
//        } finally {
//            AdmTokenThreadLocal.remove();
//        }
//    }
//}
